package isec.amov.tp2.automaintenance;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DBAdapter {
	
	private SQLiteDatabase database;
	private DbHelper dbHelper;
	
	//Load Constants from DbHelper class
	private String[] allColumns = { DbHelper.ID, DbHelper.MARCA, DbHelper.MODELO, DbHelper.MATRICULA,
			DbHelper.QUILOMETROS};
	private String[] ColumnsDetalhes = { DbHelper.ID, DbHelper.TIPO, DbHelper.ANO, DbHelper.MES, DbHelper.DIA,
			DbHelper.QUILOMETROS};

	//Constructor
	public DBAdapter(Context context) {          
		dbHelper = new DbHelper(context);
		database = dbHelper.getWritableDatabase();
		
	}
	
	//Creates a new car in the DB
	public long createCarro(Carro car) { 
        
		ContentValues values = new ContentValues(); 
        
		values.put(DbHelper.MARCA, car.getMarca()); 
        values.put(DbHelper.MODELO, car.getModelo()); 
        values.put(DbHelper.MATRICULA, car.getMatricula()); 
        values.put(DbHelper.QUILOMETROS, car.getQuilometros()); 
       
        return database.insert(DbHelper.TABLE_NAME1, null, values);
	}
	
	//Creates a new detalhe in the DB
	public long criaDetalhe(Detalhe det) { 
        
		ContentValues values = new ContentValues(); 
        
		values.put(DbHelper.TIPO, det.getTipo()); 
        values.put(DbHelper.ANO, det.getAno());
        values.put(DbHelper.MES, det.getMes());
        values.put(DbHelper.DIA, det.getDia());
        values.put(DbHelper.QUILOMETROS, det.getQuilometros());
       
        return database.insert(DbHelper.TABLE_NAME2, null, values);
	}
	
	//Converts a Cursor (pointer) into an Carro
	private Carro cursorToCarro(Cursor cursor) { 

        Carro car = new Carro(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getDouble(4)); 
        return car; 
	}
	
	//Converts a Cursor (pointer) into an Detalhe
	private Detalhe cursorToDetalhe(Cursor cursor){
		Detalhe det = new Detalhe(cursor.getInt(0), cursor.getString(1), cursor.getInt(2), cursor.getInt(3), cursor.getInt(4), cursor.getDouble(5));
		return det;
	}
	
	//Deletes the Detalhe with the id "id"
	public void deleteDetalhe (int id){ 
        database.delete(DbHelper.TABLE_NAME2, DbHelper.ID + " = " + id, null); 
	}
	
	//Returns an Detalhe from the DB
	public Detalhe getDetalhe( int id ){ 
		Cursor cursor = database.query(DbHelper.TABLE_NAME2, ColumnsDetalhes, DbHelper.ID + " = " + 
		           id, null,null, null, null);
		
		//If the id is not found returns null
		if ( cursor.getCount() == 1 )
		{
			cursor.moveToFirst(); 
			return cursorToDetalhe(cursor);
		}
		return null;
	}
	
	//Deletes the Carro with the id "id"
	public void deleteCarro (int id){ 
        database.delete(DbHelper.TABLE_NAME1, DbHelper.ID + " = " + id, null); 
	}
	
	//Returns an Carro from the DB
	public Carro getCarro( int id ){ 
		Cursor cursor = database.query(DbHelper.TABLE_NAME1, allColumns, DbHelper.ID + " = " + 
		           id, null,null, null, null);
		
		//If the id is not found returns null
		if ( cursor.getCount() == 1 )
		{
			cursor.moveToFirst(); 
			return cursorToCarro(cursor);
		}
		return null;
	}
	
	//Update an already existing Detalhe in the DB
	public long updateDetalhe(Detalhe det, int id) { 
        
		ContentValues values = new ContentValues(); 
        
		values.put(DbHelper.TIPO, det.getTipo()); 
        values.put(DbHelper.ANO, det.getAno());
        values.put(DbHelper.MES, det.getMes());
        values.put(DbHelper.DIA, det.getDia());
        values.put(DbHelper.QUILOMETROS, det.getQuilometros());
        
        return database.update(DbHelper.TABLE_NAME2, values, DbHelper.ID + " = " + id, null);
	}
	
	
	//Update an already existing Car in the DB
	public long updateCarro(Carro car, int id) { 
        
		ContentValues values = new ContentValues(); 
        
		values.put(DbHelper.MARCA, car.getMarca()); 
        values.put(DbHelper.MODELO, car.getModelo()); 
        values.put(DbHelper.MATRICULA, car.getMatricula()); 
        values.put(DbHelper.QUILOMETROS, car.getQuilometros()); 
        
        return database.update(DbHelper.TABLE_NAME1, values, DbHelper.ID + " = " + id, null);
	}

	//Opens DB connection
	public SQLiteDatabase open(Context c) {
		dbHelper = new DbHelper(c);
		return dbHelper.getWritableDatabase();
	}
	
	//Check if a matricula is already in the DB
	public boolean isTipoRepeated(String tipo){
		String []sTipo = {tipo};
		Cursor c = database.query(DbHelper.TABLE_NAME2, null, DbHelper.TIPO + " = ?", sTipo, null, null, null);
		if (c.getCount() == 0){
			c.close();
			return false;
		}
		c.close();
		return true;
	}
	
	//Check if a matricula is already in the DB
	public boolean isMatriculaRepeated(String matricula){
		String []sMatricula = {matricula};
		Cursor c = database.query(DbHelper.TABLE_NAME1, null, DbHelper.MATRICULA + " = ?", sMatricula, null, null, null);
		if (c.getCount() == 0){
			c.close();
			return false;
		}
		c.close();
		return true;
	}
	
	//Returns next available id da tabela detalhe
	public int getNextAvailableIDDetalhe(){
		int id;
		Cursor cCount = database.query(DbHelper.TABLE_NAME2, null, null, null, null, null, null);
		if(cCount.getCount() > 0) {
			cCount.moveToLast();
			id = cCount.getInt(0) + 1;
			cCount.close();
			return id;
		}
		cCount.close();
		return 1;
	}

	//Returns next available id
	public int getNextAvailableID(){
		int id;
		Cursor cCount = database.query(DbHelper.TABLE_NAME1, null, null, null, null, null, null);
		if(cCount.getCount() > 0) {
			cCount.moveToLast();
			id = cCount.getInt(0) + 1;
			cCount.close();
			return id;
		}
		cCount.close();
		return 1;
	}
	
	public int numDetalhes(){
		Cursor cCount = database.query(DbHelper.TABLE_NAME2, null, null, null, null, null, null);
		int num = cCount.getCount();
		cCount.close();
		return num;	
	}
	
	public int numCarros(){
		Cursor cCount = database.query(DbHelper.TABLE_NAME1, null, null, null, null, null, null);
		int num = cCount.getCount();
		cCount.close();
		return num;	
	}
	
	public Cursor getCarros(){
		Cursor cursor = database.rawQuery("select marca, modelo, matricula, quilometros from carro", null);
		return cursor;
	}
	
	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}
}
